package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.BaseOrder;
import com.itheima.bean.Orders;
import com.itheima.common.OrderStatus;
import com.itheima.common.R;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){

        int row = orderService.add(orders);

        if (row>0){
           return R.success("添加订单成功!");
        }

        return R.error("添加订单失败!");

    }

    /**
     * 分页查询到用户端
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R userPage(int page,int pageSize){
        IPage<Orders> page1 = orderService.findPage(page, pageSize);
        return R.success(page1);
    }

    /**
     * 分页查询订单到客户端
     * @param baseOrder
     * @return
     */
    @GetMapping("/page")
    public R page(BaseOrder baseOrder){

        IPage<Orders> page = orderService.page(baseOrder);

        return R.success(page);

    }

    /**
     * 修改订单状态
     * @param orderStatus
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody OrderStatus orderStatus){

        int row = orderService.updateStatus(orderStatus);

        if(row > 0){
            return R.success("状态修改成功！");
        }else{
            return R.error("状态修改失败！");
        }
    }
}
